package com.boe.server.register.util;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Component
public class ResultUtil {

    private final ObjectMapper mapper;

    public ResultUtil(ObjectMapper mapper) {
        this.mapper = mapper;
    }

    public void responseJson(HttpServletResponse response, Object result) {
        try {
            response.setStatus(200);
            response.setContentType("application/json");
            response.setCharacterEncoding("utf-8");
            response.getWriter().print(mapper.writeValueAsString(result));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public Map<String, Object> resultCode(int code, String msg) {
        Map<String, Object> res = new HashMap<>();
        res.put("code", code);
        res.put("msg", msg);
        return res;
    }
}
